<?php
/**
 * 订单管理
 */

namespace Admin\Controller;
use Admin\BaseController;
use Common\Model\BusinessModel;
use Common\Model\OrderModel;
use Common\Model\RoomModel;
use Common\Model\UserModel;
use Common\Model\UtilsModel;
use Common\Model\WeixinModel;
use Think\Controller;
use Think\Db;
use Think\Page;

class OrderController extends BaseController
{






    /**
     * 订单列表
     */
    public function OrderList(){

        $where  = '1';


        $list =OrderModel::getOrderInstance()
            ->where($where)
            ->order('oid desc')
            ->select();



        $this->smarty->assign('list', $list);

        //$auditStatusKV=UserModel::getAuditStatus();

        //$this->smarty->assign('auditType', $auditStatusKV);
        //$this->smarty->assign('selectedAuditType', $auditStatus);


        $this->smarty->assign('statusKv',OrderModel::getStatusKv());
        $this->smarty->assign('roomKv',RoomModel::getRoomKv());

        $this->smarty->assign('hasPaid',OrderModel::ORDER_STATUS_PAID);
        //$this->smarty->assign('isOnline',RoomModel::ROOM_ONLINE);

        $this->smarty->display('Order/order_list.html');
    }


    /**
     * 退款显示
     */
    public function refundView(){
        $oid=I('get.oid',0,'intval');
        $orderInfo=OrderModel::getOrderInstance()->where('oid='.$oid)->find();
        if(!$orderInfo){
            $this->error('指定订单不存在');
        }
        if($orderInfo['status']==OrderModel::ORDER_STATUS_UNPAY){
            $this->error('订单未支付，不可退款');
        }
        if($orderInfo['status']==OrderModel::ORDER_STATUS_CANCEL){
            $this->error('订单已取消，不可退款');
        }

        $dayPrice=json_decode($orderInfo['dayPrice'],1);
        $now=time();
        $refundMoney=array();
       foreach ($dayPrice as $day=>$price){
           $dayTime=strtotime($day)+UtilsModel::ONE_HOUR * 12;
           if($dayTime<=$now){
               $refundMoney[$day]=0;
           }elseif (($dayTime - $now)/UtilsModel::ONE_DAY>=CANCEL_RETURN_DAYS){
               $refundMoney[$day]=$price;
           }else{
               $refundMoney[$day]=round($price * CANCEL_RETURN_SCALE /100);
           }
       }
        $this->smarty->assign('oid',$oid);
        $this->smarty->assign('now',$now);
        $this->smarty->assign('totalMoney',$orderInfo['totalMoney']);
        $this->smarty->assign('refundArray',$refundMoney);
        $this->smarty->assign('dayPrice',$dayPrice);
        $this->smarty->display('Order/refund.html');


    }
    

    public function refundDo()
    {
        $oid = I('post.oid', 0, 'intval');
        $refund = I('post.refund', 0, 'floatval');
        $desc = I('post.desc');
        if (!$oid || $refund <= 0) {
            $this->ajaxReturn(array(
                'status' => 1,
                'msg' => '参数传递错误'
            ));
        }
        $desc = mb_substr($desc, 0, 400);

        $orderInfo = OrderModel::getOrderInstance()->where('oid=' . $oid)->find();

        if (!$orderInfo) {
            $this->ajaxReturn(array(
                'status' => 1,
                'msg' => '参数传递错误'
            ));
        }
        if ($orderInfo['status'] == OrderModel::ORDER_STATUS_UNPAY) {
            $this->ajaxReturn(array(
                'status' => 1,
                'msg' => '订单未支付，不可退款'
            ));

        }
        if ($orderInfo['status'] == OrderModel::ORDER_STATUS_CANCEL) {
            $this->ajaxReturn(array(
                'status' => 1,
                'msg' => '订单已取消，不可退款'
            ));
        }

        if ($refund > $orderInfo['totalMoney']) {
            $this->ajaxReturn(array(
                'status' => 1,
                'msg' => '退款金额不可大于订单金额'
            ));
        }


        OrderModel::getRefundInstance()->startTrans();
        $refundId = OrderModel::getRefundInstance()->add(
            array(
                'oid' => $oid,
                'refundMoney' => $refund,
                'desc' => $desc,
                'status' => OrderModel::REFUND_STATUS_APPLY,
                'addTime' => time()
            )
        );
        if ($refundId === false) {
            OrderModel::getRefundInstance()->rollback();
            $this->ajaxReturn(array(
                'status' => 1,
                'msg' => '退款发起失败'
            ));
        }
        $result = WeixinModel::refund(array(
            'refundNo' => $refundId,
            'outTradeNo' => $oid,
            'total' => $orderInfo['totalMoney'],
            'refund' => $refund
        ));

        if ($result['status'] == 0){
            OrderModel::getRefundInstance()->commit();
            $this->ajaxReturn(array(
                'status' => 0,
                'msg' => '退款发起成功'
            ));
        }else{
            OrderModel::getRefundInstance()->rollback();
            $this->ajaxReturn($result);
        }

    }


}
